<?php
/**
 * Created by SNWAY.
 * User: w
 * Date: 2017/6/19
 * Time: 10:49
 */

namespace app\index\controller;

use app\index\Controller;
use think\Response;
use app\index\model\Files;

class Upload
{
    public function  index()
    {
        return view('index');
    }
    //上传操作
    public function upload(){
        /*** 获取表单上传文件 例如上传了001.jpg***/
        $img = IMG_PATH;	//配置图片域名
        $file = request()->file('image');
        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->validate(['size'=>10485760,'ext'=>'jpg,png,gif'])->move( URL_PATH."/thumbFile/");
        if($info){
            // 成功上传后 获取上传信息
            $url= $img ."/thumbFile/".$info->getSaveName();
//            $url='/uploads/'.$info->getSaveName();
            $files = new Files();
            $aid=autoId();
            $data=[
                'filesId'=>$aid,
                'thumbFile'=>$url,
                'ip'=>ip(),
                'addDateTime'=>aTime(),
                'operator'=>getInputPerson(),
            ];
            $ret = $files->isUpdate(false)->save($data);
            if($ret==1){
               echo json_encode(["code" => 0, "msg" => $url, "data" => $aid]);
            }else{
                echo json_encode(["code" => 1, "msg" => '上传出现错误，请重试。', "data" => '']);
            }
        }else{
            // 上传失败获取错误信息
            echo json_encode(["code" => 1, "msg" =>  '上传数据大小错误。', "data" => '']);
        }/*******/
    }

    protected function mkUploadDir($dir = '', $chmod = 0777)
    {
        if (is_dir($dir) || @mkdir($dir))
            return true;
        if (!mkUploadDir(dirname($dir)))
            return false;
        return @mkdir($dir);
    }
    public function  indexAll()
    {
        return view('indexall');
    }
    //指定上传文件类型
    public function uploadAll(){
        $fileType=$_GET['fileType'];
        $type=$_GET['type'];
        /*** 获取表单上传文件 例如上传了001.jpg***/
        $img = IMG_PATH;	//配置图片域名
        $file = request()->file('image');
        $uploadInfo = $file->getInfo();
        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->validate(['size'=>52428800,'ext'=>$fileType])->move(URL_PATH."/thumbFile/");
        if($type=="service"){
            $this->mkUploadDir(URL_PATH."/detailImgFile/");
            $this->mkUploadDir(URL_PATH."/detailImgFile/".date("Ymd"));
            if($info){
                $url= $img."thumbFile/" .$info->getSaveName();
                $type=substr(strrchr($url,"."),1);
                list($img_width, $img_height)=getimagesize($url);
                $new_detail_width=Db::name('param')->where('paramCode','THUMBNAIL_WIDTH')->value('paramValue');
                $new_detail_height=Db::name('param')->where('paramCode','THUMBNAIL_HEIGHT')->value('paramValue');
                $new_detail=imagecreatetruecolor($new_detail_width, $new_detail_height);
                if($type=="jpg"){
                    //二级图片样式
                    $img_file=imagecreatefromjpeg($url);
//图像输出新图片、另存为
                    $imgFile=date("Ymd")."/".md5(date("YmdHis").$info->getSaveName()).".jpg";
//                $uploadfile=URL_PATH.$name;
                    //详情图片样式
//copy部分图像并调整
                    imagecopyresampled($new_detail, $img_file,0, 0,0, 0,$new_detail_width, $new_detail_height, $img_width, $img_height);
//图像输出新图片、另存为
                    imagejpeg($new_detail, URL_PATH."/detailImgFile"."/".$imgFile);

                }
                if($type=="png"){

                    $img_file=imagecreatefrompng($url);
//图像输出新图片、另存为
                    $imgFile=date("Ymd")."/".md5(date("YmdHis").$info->getSaveName()).".png";
                    //详情图片样式
//copy部分图像并调整
                    imagecopyresampled($new_detail, $img_file,0, 0,0, 0,$new_detail_width, $new_detail_height, $img_width, $img_height);
//图像输出新图片、另存为
                    imagepng($new_detail, URL_PATH."/detailImgFile"."/".$imgFile);
                }
                if($type=="gif"){
                    $img_file=imagecreatefromgif($url);
//图像输出新图片、另存为
                    //详情图片样式
                    imagecopyresampled($new_detail, $img_file,0, 0,0, 0,$new_detail_width, $new_detail_height, $img_width, $img_height);
//图像输出新图片、另存为
                    $imgFile=date("Ymd")."/".md5(date("YmdHis").$info->getSaveName()).".gif";
                    imagegif($new_detail, URL_PATH."/detailImgFile"."/".$imgFile);

                }
                $files = new Files();
                $aid=autoId();
                $data=[
                    'filesId'=>$aid,
                    'thumbFile'=>$url,
                    'detailImgFile'=>$img."detailImgFile/" .$imgFile,
                    'ip'=>ip(),
                    'operator'=>getInputPerson()
                ];
                $ret = $files->isUpdate(false)->save($data);

                if($ret==1){
                    echo json_encode(["code" => 0, "msg" => $url, "data" => $aid, "fileName" => $uploadInfo['name']]);
                }else{
                    echo json_encode(["code" => 1, "msg" => '上传出现错误，请重试。', "data" => '']);
                }
            }else{
                // 上传失败获取错误信息
                // return Response::create(["code" => 1, "msg" => $file->getError(), "data" => ''], 'html');
                echo json_encode(["code" => 1, "msg" => $file->getError(), "data" => '']);
            }/*******/
        }else{
            if($info){
                // 成功上传后 获取上传信息
//            $url= $img . 'img' . DS .$info->getSaveName();
                $url= $img."thumbFile/" .$info->getSaveName();
                $files = new Files();
                $aid=autoId();
                $data=[
                    'filesId'=>$aid,
                    'thumbFile'=>$url,
                    'ip'=>ip(),
                    'operator'=>getInputPerson()
                ];
                $ret = $files->isUpdate(false)->save($data);
                if($ret==1){
                    echo json_encode(["code" => 0, "msg" => $url, "data" => $aid,'fileName'=>$uploadInfo['name']]);
                }else{
                    echo json_encode(["code" => 1, "msg" => '上传出现错误，请重试。', "data" => '']);
                }
            }else{
                // 上传失败获取错误信息
                // return Response::create(["code" => 1, "msg" => $file->getError(), "data" => ''], 'html');
                echo json_encode(["code" => 1, "msg" => $file->getError(), "data" => '']);
            }/*******/
        }
//        $this->mkUploadDir(URL_PATH."/thumbFile/");



    }

}